package product;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;

import java.awt.Image;
import java.awt.SystemColor;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.net.URL;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.Icon;
import javax.swing.border.EmptyBorder;

public class SetLogin extends JPanel {
	private JPanel panel;
	private JLabel label;
	private JPanel panel_1;
	private JLabel lblNewLabel;
	private JPanel panel_2;
	private JPanel panel_3;
	private JLabel lblNewLabel_1;
	private JPanel panel_4;
	private JLabel lblNewLabel_2;
	private JButton btnNewButton;
	private JLabel lblNewLabel_3;
	private JLabel lblNewLabel_4;
	
	int level = 2;
	Login login; 

	/**
	 * Create the panel.
	 */
	public SetLogin(int level, Login l) {
		
		this.level = level; // Login 프레임으로부터 데이터를 얻어온다.
		this.login = l; // Login 프레임을 얻어온다.
		
		setBackground(new Color(0, 0, 102));
		setLayout(new BorderLayout(0, 0));
		add(getPanel(), BorderLayout.NORTH);
		add(getPanel_1(), BorderLayout.SOUTH);
		add(getPanel_2(), BorderLayout.CENTER);
		

	}
	
	public void setLabel(String text){
		this.label.setText(text);
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(Color.BLACK);
			panel.add(getLabel());
		}
		return panel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("로그인 하였습니다.");
			label.setForeground(Color.WHITE);
			label.setFont(new Font("굴림", Font.PLAIN, 11));
		}
		return label;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBackground(new Color(0, 0, 102));
			panel_1.add(getLblNewLabel());
		}
		return panel_1;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("     ");
		}
		return lblNewLabel;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setBackground(new Color(0, 0, 102));
			panel_2.setLayout(new BorderLayout(0, 0));
			panel_2.add(getPanel_3(), BorderLayout.NORTH);
			panel_2.add(getPanel_4(), BorderLayout.SOUTH);
			panel_2.add(getBtnNewButton(), BorderLayout.CENTER);
			panel_2.add(getLblNewLabel_3(), BorderLayout.WEST);
			panel_2.add(getLblNewLabel_4(), BorderLayout.EAST);
		}
		return panel_2;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.setBackground(new Color(0, 0, 102));
			panel_3.add(getLblNewLabel_1());
		}
		return panel_3;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("    ");
		}
		return lblNewLabel_1;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.setBackground(new Color(0, 0, 102));
			panel_4.add(getLblNewLabel_2());
		}
		return panel_4;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("     ");
		}
		return lblNewLabel_2;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			
			URL url = null; 
			Image img = null;
			
			if (level == 1){
				url = getClass().getClassLoader().getResource("adminmode.png");
				img = Toolkit.getDefaultToolkit().getImage(url);
			}
			else{
				url = getClass().getClassLoader().getResource("membermode.png");
				img = Toolkit.getDefaultToolkit().getImage(url);
			}
			
			btnNewButton = new JButton(new ImageIcon(img));
			
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					if (level == 1){
						// 관리자 모드 
						login.panel_10.removeAll();
						Admin ad = new Admin(login.conn);
						login.panel_10.add(ad);
						login.panel_10.updateUI();
						login.setTitle("관리자 모드");
					}
					else{
						// 사원 모드 
						
						login.panel_10.removeAll();
						
						Employee em = new Employee(login.conn); // Employee 패널 생성
					
						String[] args = login.getData();
						
						em.setPersonalNumber(args[0]); // 로그인한 사용자의 사번을 employee로	
						em.setPersonalName(args[1]); // 로그인한 사용자의 이름을 employee로
						em.setStartDate(args[2]); // 로그인한 사용자의 입사날

						login.panel_10.add(em);// panel 추가해준다
						login.panel_10.updateUI();

						login.setTitle("사원 모드"); // 프레임 제목을 재설정.

						// employee 패널에 사번, 이름, 입사일을 출력해준다.
						em.number.setText(args[0] + "  "); 
						em.irum.setText(args[1] + "  ");
					
						// 입사한지 몇 년째인지 계산. 
						String sDate[] = args[2].split("-"); // 입사일을 '-' 기준으로 분리.
						String sDate2[] = sDate[2].split(" ");
						
						String start_date = sDate[0] + "-" + sDate[1] + "-" + sDate2[0];
					
						em.nal.setText("입사일 : " + start_date + "  ");
						
						String join = sDate[0]; // 분리된 스트링 중 0번째가 년(year)에 해당.

						SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
						String now = sdf.format(new Date()); // 현재 년도를 yy포맷으로 반환
						
						
						int join1 = Integer.parseInt(join);
						int now1 = Integer.parseInt(now) ;

						String ho =  Integer.toString((now1 - join1 + 1) ); // 입사한지 몇년째 된지 계산
						em.date.setText(ho+"년차  ");
						em.toto();
		
						
					}
				}
			});
			btnNewButton.setContentAreaFilled(false);
			btnNewButton.setBackground(new Color(0, 0, 102));
			btnNewButton.setBorder(null);
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("                                ");
			lblNewLabel_3.setBackground(new Color(0, 0, 102));
		}
		return lblNewLabel_3;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("                                ");
			lblNewLabel_4.setBackground(new Color(0, 0, 102));
		}
		return lblNewLabel_4;
	}
}
